﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Data.SqlClient;
public partial class EnrolEarn : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
        {
            
            this.txtMoney.Attributes["onkeyup"] = "value=value.replace(/[^\\d]/g,'')";//设置txtMoney的输入文本只能为数字
            SqlConnection conn = DB.GetCon();
            conn.Open();
            this.AutoID(conn);
            ddlPayMent.Items.Add(new ListItem("-请选择-"));
            this.bind();
        }
    }

    //根据数据库中的记录数自动产生一个数作为入库单ID
    public void AutoID(SqlConnection conn)
    {
        SqlCommand com = new SqlCommand("select count(*) from tb_Earn", conn);
        int Count = Convert.ToInt32(com.ExecuteScalar());
        this.txtID.Text = Convert.ToString(Count + 1);
        if (conn.State == ConnectionState.Open)
        {
            com.Dispose();
            conn.Close();
        }
    }
    public void bind()
    {
        DataSet ds1 = DB.reDs("select Title from tb_BigType where Type='收付款方式'");
        this.ddlType.DataSource = ds1;
        ddlType.DataTextField = "Title";
        ddlType.DataBind();
        DataSet ds2 = DB.reDs("select * from tb_Enterprise where Type3='是'");
        this.ddlPayMent.DataSource = ds2;
        ddlPayMent.DataTextField = "Name";
        ddlPayMent.DataBind();
    }
    //使页面恢复初始状态
    public void clear()
    {
        foreach (Control c in this.form1.Controls)//遍历控件查找文本框控件，并清空
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.TextBox")
            {
                TextBox t = (TextBox)c;
                t.Text = "";
            }
        }
        foreach (Control c in this.form1.Controls)//遍历控件查找DropDownList控件，并恢复默认选项
        {
            if (c.GetType().ToString() == "System.Web.UI.WebControls.DropDownList")
            {
                DropDownList d = (DropDownList)c;
                d.SelectedIndex = -1;
            }
        }
    }
    protected void btnOK_Click(object sender, EventArgs e)
    {
        string ID = this.txtID.Text.Trim();
        string Cause = this.txtCause.Text.Trim();
        string Type = this.ddlType.SelectedValue.ToString();
        string Money = this.txtMoney.Text.Trim();
        string Owe=this.txtOwe .Text;
        string Payment = this.ddlPayMent.SelectedValue.ToString();
        string PayDate = this.txtPayDate.Text.Trim();
        string Sign = this.txtSign.Text.Trim();
        string Remark = this.txtRemark.Text.Trim();
        try
        {
            DB.ExSql("insert into tb_Earn values('" + ID + "','" + Cause + "','" + Type + "'," + Money +","+ Owe + ",'" + Payment + "','" + PayDate + "','" + Sign + "','" + Remark + "')");
            //使页面恢复初始状态
            this.clear();
            SqlConnection conn = DB.GetCon();
            conn.Open();
            AutoID(conn);//根据数据库中的记录数自动产生一个数作为入库单ID
        }
        catch (SqlException oe)
        {
           Response.Write( oe.Message.ToString());
        }
    }
    protected void ddlPayMent_SelectedIndexChanged(object sender, EventArgs e)
    {
        if (ddlPayMent.SelectedValue != "－请选择－")
        {
            DataTable dt = DB.ExcuteDataTable("select OutID from tb_Out where Name='" + this.ddlPayMent.SelectedValue + "'");
            DataTable dtquit = DB.ExcuteDataTable("select sum(Gathering) from tb_QuitDetails where Name='" + this.ddlPayMent.SelectedValue + "'");
            decimal outGathering = 0;
            decimal quitGathering = 0;
            decimal money = 0;
            foreach (DataRow dr in dt.Rows)
            {
                string OutID =dr[0].ToString();
                DataTable dtout = DB.ExcuteDataTable("select Gathering from tb_OutDetails where OutID="+OutID);
                if (dtout.Rows .Count !=0)
                {
                    if(dtout.Rows[0][0].ToString() != "")
                        outGathering += Convert.ToDecimal(dtout.Rows[0][0].ToString());
                }
            }
            if (dtquit.Rows[0][0].ToString() != "")
            {
                quitGathering = Convert.ToDecimal(dtquit.Rows[0][0].ToString());
            }
            if (this.txtMoney.Text != "")
            {
                money = Convert.ToDecimal(this.txtMoney.Text);
            }
            this.txtOwe.Text = Convert.ToString(outGathering + quitGathering - money);
        }
    }
}
